<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>OS模块 | MXY 1.0版Python整理</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/7.常用模块/4.zipfile模块.html" />
    
    
    <link rel="prev" href="../../home/7.常用模块/2.math模块.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7.3"
        data-chapter-title="OS模块"
        data-filepath="home/7.常用模块/3.OS模块.md"
        data-basepath="../.."
        data-revision="Mon Oct 22 2018 08:32:35 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        MXY整理版Python基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" >
            
            <span><b>1.</b> Python的安装和简介</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="home/1.Python的安装和简介/1.python基础知识.html">
            
                
                    <a href="../../home/1.Python的安装和简介/1.python基础知识.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Python的基础知识
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="home/1.Python的安装和简介/2.python的安装及版本检测.html">
            
                
                    <a href="../../home/1.Python的安装和简介/2.python的安装及版本检测.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Python安装及版本检测
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="home/1.Python的安装和简介/3.第一个python程序.html">
            
                
                    <a href="../../home/1.Python的安装和简介/3.第一个python程序.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        第一个Python程序
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" >
            
            <span><b>2.</b> Python的基本语法</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/2.Python的基本语法/1.注释及语句分类.html">
            
                
                    <a href="../../home/2.Python的基本语法/1.注释及语句分类.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        注释及语句分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/2.Python的基本语法/2.命名方式和关键字.html">
            
                
                    <a href="../../home/2.Python的基本语法/2.命名方式和关键字.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        命名方式和关键字
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" >
            
            <span><b>3.</b> Python的变量及数据类型</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/3.变量和数据类型/1.变量.html">
            
                
                    <a href="../../home/3.变量和数据类型/1.变量.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/3.变量和数据类型/2.数据类型.html">
            
                
                    <a href="../../home/3.变量和数据类型/2.数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/3.变量和数据类型/3.数据类型转换.html">
            
                
                    <a href="../../home/3.变量和数据类型/3.数据类型转换.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        数据类型转换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="home/3.变量和数据类型/4.运算和运算符.html">
            
                
                    <a href="../../home/3.变量和数据类型/4.运算和运算符.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        数据类型转换
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" >
            
            <span><b>4.</b> Python的流程控制</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="home/4.流程控制/1.顺序结构和分支结构.html">
            
                
                    <a href="../../home/4.流程控制/1.顺序结构和分支结构.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        顺序结构和分支结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="home/4.流程控制/2.循环结构.html">
            
                
                    <a href="../../home/4.流程控制/2.循环结构.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        循环结构
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" >
            
            <span><b>5.</b> 函数及作用域</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="home/5.函数及作用域/1.函数.html">
            
                
                    <a href="../../home/5.函数及作用域/1.函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="home/5.函数及作用域/2.函数文档.html">
            
                
                    <a href="../../home/5.函数及作用域/2.函数文档.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        函数文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="home/5.函数及作用域/3.变量的作用域.html">
            
                
                    <a href="../../home/5.函数及作用域/3.变量的作用域.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        变量的作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="home/5.函数及作用域/4.内部函数和闭包操作.html">
            
                
                    <a href="../../home/5.函数及作用域/4.内部函数和闭包操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        内部函数和闭包操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="home/5.函数及作用域/5.lambda表达式.html">
            
                
                    <a href="../../home/5.函数及作用域/5.lambda表达式.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        lambda表达式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="home/5.函数及作用域/6.高阶函数.html">
            
                
                    <a href="../../home/5.函数及作用域/6.高阶函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        高阶函数
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" >
            
            <span><b>6.</b> 内置函数及操作</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="home/6.内置函数及操作/1.字符串相关.html">
            
                
                    <a href="../../home/6.内置函数及操作/1.字符串相关.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        字符串相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="home/6.内置函数及操作/2.format格式字符串.html">
            
                
                    <a href="../../home/6.内置函数及操作/2.format格式字符串.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        format格式字符串
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="home/6.内置函数及操作/3.内建函数.html">
            
                
                    <a href="../../home/6.内置函数及操作/3.内建函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="home/6.内置函数及操作/4.列表.html">
            
                
                    <a href="../../home/6.内置函数及操作/4.列表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        列表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="home/6.内置函数及操作/5.元组.html">
            
                
                    <a href="../../home/6.内置函数及操作/5.元组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        元组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="home/6.内置函数及操作/6.字典.html">
            
                
                    <a href="../../home/6.内置函数及操作/6.字典.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        字典
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="home/6.内置函数及操作/7.集合.html">
            
                
                    <a href="../../home/6.内置函数及操作/7.集合.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        集合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="home/6.内置函数及操作/8.文件操作.html">
            
                
                    <a href="../../home/6.内置函数及操作/8.文件操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        文件操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" >
            
            <span><b>7.</b> 常用的模块</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="home/7.常用模块/1.String模块.html">
            
                
                    <a href="../../home/7.常用模块/1.String模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        String模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="home/7.常用模块/2.math模块.html">
            
                
                    <a href="../../home/7.常用模块/2.math模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        math模块
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="7.3" data-path="home/7.常用模块/3.OS模块.html">
            
                
                    <a href="../../home/7.常用模块/3.OS模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        OS模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="home/7.常用模块/4.zipfile模块.html">
            
                
                    <a href="../../home/7.常用模块/4.zipfile模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        zipfile模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="home/7.常用模块/5.tarfile模块.html">
            
                
                    <a href="../../home/7.常用模块/5.tarfile模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        tarfile模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="home/7.常用模块/6.shutil模块.html">
            
                
                    <a href="../../home/7.常用模块/6.shutil模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        shutil模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.7" data-path="home/7.常用模块/7.时间相关模块.html">
            
                
                    <a href="../../home/7.常用模块/7.时间相关模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.7.</b>
                        
                        时间相关模块
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" >
            
            <span><b>8.</b> 面向对象</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="home/8.面向对象/1.类和对象-上.html">
            
                
                    <a href="../../home/8.面向对象/1.类和对象-上.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        类和对象-上
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="home/8.面向对象/2.类和对象-下.html">
            
                
                    <a href="../../home/8.面向对象/2.类和对象-下.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        类和对象-下
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="home/8.面向对象/3.魔术方法.html">
            
                
                    <a href="../../home/8.面向对象/3.魔术方法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        魔术方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="home/8.面向对象/4.装饰器.html">
            
                
                    <a href="../../home/8.面向对象/4.装饰器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="home/8.面向对象/5.抽象类.html">
            
                
                    <a href="../../home/8.面向对象/5.抽象类.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        抽象类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="home/8.面向对象/6.错误和异常处理.html">
            
                
                    <a href="../../home/8.面向对象/6.错误和异常处理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        错误和异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="home/8.面向对象/7.模块和包.html">
            
                
                    <a href="../../home/8.面向对象/7.模块和包.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        模块和包
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" >
            
            <span><b>9.</b> tkinter模块</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="home/9.tkinter模块/tkinter模块.html">
            
                
                    <a href="../../home/9.tkinter模块/tkinter模块.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        tkinter模块
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="home/10.网络编程/1.网络编程.html">
            
                
                    <a href="../../home/10.网络编程/1.网络编程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        网络编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="home/10.网络编程/2.TCP编程.html">
            
                
                    <a href="../../home/10.网络编程/2.TCP编程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        TCP编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="home/10.网络编程/3.UDP编程.html">
            
                
                    <a href="../../home/10.网络编程/3.UDP编程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        UDP编程
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="home/11.多任务/多任务原理.html">
            
                
                    <a href="../../home/11.多任务/多任务原理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        多任务
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="11.1" data-path="home/11.多任务/1.多进程实现多任务.html">
            
                
                    <a href="../../home/11.多任务/1.多进程实现多任务.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.1.</b>
                        
                        多进程实现多任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="home/11.多任务/2.多线程实现多任务.html">
            
                
                    <a href="../../home/11.多任务/2.多线程实现多任务.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.2.</b>
                        
                        多线程实现多任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11.3" data-path="home/11.多任务/3.协程实现多任务.html">
            
                
                    <a href="../../home/11.多任务/3.协程实现多任务.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.3.</b>
                        
                        协程实现多任务
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="12" >
            
            <span><b>12.</b> Python实战项目</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="home/12.Python实战项目/1.简易计算器.html">
            
                
                    <a href="../../home/12.Python实战项目/1.简易计算器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        简易计算器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="home/12.Python实战项目/2.自动售货机.html">
            
                
                    <a href="../../home/12.Python实战项目/2.自动售货机.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        自动售货机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="home/12.Python实战项目/3.银行项目.html">
            
                
                    <a href="../../home/12.Python实战项目/3.银行项目.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.3.</b>
                        
                        银行项目
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.4" data-path="home/12.Python实战项目/4.简单的登录注册.html">
            
                
                    <a href="../../home/12.Python实战项目/4.简单的登录注册.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.4.</b>
                        
                        简单的登录注册
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.5" data-path="home/12.Python实战项目/5.双色球.html">
            
                
                    <a href="../../home/12.Python实战项目/5.双色球.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.5.</b>
                        
                        双色球
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.6" data-path="home/12.Python实战项目/6.python垃圾回收机制.html">
            
                
                    <a href="../../home/12.Python实战项目/6.python垃圾回收机制.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.6.</b>
                        
                        python垃圾回收机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.7" data-path="home/12.Python实战项目/7.turtle画国旗.html">
            
                
                    <a href="../../home/12.Python实战项目/7.turtle画国旗.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.7.</b>
                        
                        turtle画国旗
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.8" data-path="home/12.Python实战项目/8.turtle画小猪佩奇.html">
            
                
                    <a href="../../home/12.Python实战项目/8.turtle画小猪佩奇.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.8.</b>
                        
                        turtle画小猪佩奇
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.9" data-path="home/12.Python实战项目/9.turtle画个表.html">
            
                
                    <a href="../../home/12.Python实战项目/9.turtle画个表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.9.</b>
                        
                        turtle画个表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >MXY 1.0版Python整理</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="os&#x6A21;&#x5757;">OS&#x6A21;&#x5757;</h1>
<hr>
<p>&#x4E8B;&#x4EF6;&#x6A21;&#x5757; os&#x6A21;&#x5757; &#x4E24;&#x4E2A;&#x9898;</p>
<blockquote>
<p>OS &#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x7684;&#x7B80;&#x79F0;</p>
<p>os&#x6A21;&#x5757;&#x5C31;&#x662F;&#x5BF9;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x4F7F;&#x7528;&#x8BE5;&#x6A21;&#x5757;&#x5FC5;&#x987B;&#x5148;&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF1A;
import os</p>
</blockquote>
<ul>
<li><h2 id="os&#x6A21;&#x5757;&#x51FD;&#x6570;">os&#x6A21;&#x5757;&#x51FD;&#x6570;</h2>
<ul>
<li><h4 id="getcwd">getcwd()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;
  &#x683C;&#x5F0F;&#xFF1A;os.getcwd()
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x8DEF;&#x5F84;&#x5B57;&#x7B26;&#x4E32;
</code></pre>
</li>
<li><h4 id="chdir">chdir()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x4FEE;&#x6539;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;
  &#x683C;&#x5F0F;:os.chdir()
  &#x8FD4;&#x56DE;&#x503C;:<span class="hljs-keyword">None</span>
</code></pre>
</li>
<li><h4 id="listdir">listdir()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;&#x7EC4;&#x6210;&#x7684;&#x5217;&#x8868;
  &#x683C;&#x5F0F;:os.listdir(&#x76EE;&#x5F55;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x76EE;&#x5F55;&#x4E2D;&#x5185;&#x5BB9;&#x540D;&#x79F0;&#x7684;&#x5217;&#x8868;
</code></pre>
</li>
<li><h4 id="mkdir">mkdir()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;/&#x6587;&#x4EF6;&#x5939;
  &#x683C;&#x5F0F;&#xFF1A;os.mkdir(&#x76EE;&#x5F55;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;<span class="hljs-keyword">None</span>
</code></pre>
</li>
<li><h4 id="makedirs">makedirs()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x9012;&#x5F52;&#x521B;&#x5EFA;&#x6587;&#x4EF6;&#x5939;
  &#x683C;&#x5F0F;:os.makedirs(&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;<span class="hljs-keyword">None</span>
</code></pre>
</li>
<li><h4 id="rmdir">rmdir()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x79FB;&#x9664;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF08;&#x5FC5;&#x987B;&#x662F;&#x7A7A;&#x76EE;&#x5F55;&#xFF09;
  &#x683C;&#x5F0F;&#xFF1A;os.rmdir(&#x76EE;&#x5F55;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;:<span class="hljs-keyword">None</span>
</code></pre>
</li>
<li><h4 id="removedirs">removedirs()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x9012;&#x5F52;&#x5220;&#x9664;&#x6587;&#x4EF6;&#x5939;
  &#x683C;&#x5F0F;&#xFF1A;os.removedirs(&#x76EE;&#x5F55;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;<span class="hljs-keyword">None</span>
</code></pre>
<pre><code>  &#x6CE8;&#x610F;&#xFF1A; &#x5982;&#xFF1A;&#x5220;&#x9664; D:/a/b/c

  &#x5982;&#x679C;abc&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x9664;&#x4E86;&#x8DEF;&#x5F84;&#x663E;&#x793A;&#x7684;&#x6587;&#x4EF6;&#x5939;&#x4E4B;&#x5916;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x5939;&#xFF0C;removedirs&#x4F1A;&#x79FB;&#x9664;&#x6389;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x5939;a&#xFF0C;b&#xFF0C;c
  &#x5982;&#x679C;abc&#x4EFB;&#x610F;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x5305;&#x542B;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x5219;&#x8BE5;&#x6210;&#x6587;&#x4EF6;&#x5939;&#x4E0D;&#x4F1A;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x6700;&#x5E95;&#x5C42;&#x7684;c&#x6587;&#x4EF6;&#x5939;&#x5219;&#x4F1A;&#x7206;&#x51FA;&#x975E;&#x7A7A;&#x9519;&#x8BEF;&#xFF01;
</code></pre></li>
</ul>
</li>
</ul>
<hr>
<ul>
<li><h2 id="&#x901A;&#x7528;os&#x64CD;&#x4F5C;">&#x901A;&#x7528;os&#x64CD;&#x4F5C;</h2>
<ul>
<li><h4 id="rename">rename()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x4FEE;&#x6539;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x540D;&#x79F0;
  &#x683C;&#x5F0F;&#xFF1A;os.rename(&#x6E90;&#x6587;&#x4EF6;&#x6216;&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x76EE;&#x6807;&#x6587;&#x4EF6;&#x6216;&#x6587;&#x4EF6;&#x5939;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;<span class="hljs-keyword">None</span>
</code></pre>
</li>
<li><h4 id="stat">stat()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;
  &#x683C;&#x5F0F;&#xFF1A;os.stat(&#x6587;&#x4EF6;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x5305;&#x542B;&#x6587;&#x4EF6;&#x4FE1;&#x606F;&#x7684;&#x5143;&#x7EC4;
</code></pre>
</li>
<li><h4 id="getenv">getenv()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x7CFB;&#x7EDF;&#x73AF;&#x5883;&#x53D8;&#x91CF;
  &#x683C;&#x5F0F;&#xFF1A;os.getenv(&#x83B7;&#x53D6;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x540D;&#x79F0;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;
</code></pre>
</li>
<li><h4 id="putenv">putenv()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;&#xFF1A;&#x8BBE;&#x7F6E;&#x7CFB;&#x7EDF;&#x73AF;&#x5883;&#x53D8;&#x91CF;
  &#x683C;&#x5F0F;&#xFF1A;os.putenv(<span class="hljs-string">&apos;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x540D;&#x79F0;&apos;</span>,&#x503C;)
  &#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x65E0;
  &#x6CE8;&#x610F;&#xFF1A;putenv&#x786E;&#x5B9E;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x6210;&#x529F;&#xFF0C;&#x4F46;&#x662F;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x6B63;&#x5E38;&#x7684;getenv&#x68C0;&#x6D4B;&#x5230;
</code></pre>
</li>
<li><h4 id="system">system()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5728;python&#x4E2D;&#x4F7F;&#x7528;&#x7CFB;&#x7EDF;&#x547D;&#x4EE4;
  &#x683C;&#x5F0F;: os.system(&#x7CFB;&#x7EDF;&#x547D;&#x4EE4;)
  &#x8FD4;&#x56DE;&#x503C;: &#x7CFB;&#x7EDF;&#x547D;&#x4EE4;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;

  # &#x8FD0;&#x884C;&#x7CFB;&#x7EDF;&#x547D;&#x4EE4;
  os.system(&apos;cd /usr/local&apos;)
  os.mkdir(&apos;aaa.txt)

  # &#x8FD0;&#x884C;shell&#x547D;&#x4EE4;
  # &#x4F8B;&#xFF1A;&#x6253;&#x5F00;&#x8BB0;&#x4E8B;&#x672C;
  os.system(&quot;notepad&quot;)
  # &#x4F8B;&#xFF1A;&#x6253;&#x5F00;&#x5199;&#x5B57;&#x677F;
  os.system(&quot;write&quot;)
  # &#x4F8B;&#xFF1A;&#x6253;&#x5F00;&#x753B;&#x677F;
  os.system(&quot;mspaint&quot;)
  # &#x4F8B;&#xFF1A;&#x6253;&#x5F00;&#x7CFB;&#x7EDF;&#x8BBE;&#x7F6E;
  os.system(&quot;msconfig&quot;)
  # &#x4F8B;&#xFF1A;&#x6253;&#x5F00;&#x7CFB;&#x7EDF;&#x6CE8;&#x518C;&#x8868;
  os.system(&quot;regedit&quot;)
  # &#x5173;&#x673A;&#x64CD;&#x4F5C;......500&#x8868;&#x793A;500s&#x540E;&#x5173;&#x673A;
  os.system(&quot;shutdown -s -t 500&quot;)
  # &#x53D6;&#x6D88;&#x5173;&#x673A;
  os.system(&quot;shutdown -a&quot;)
  # &#x5173;&#x95ED;&#x67D0;&#x4E2A;.exe
  os.system(&quot;taskkill /f /im notepad.exe&quot;)
</code></pre>
</li>
</ul>
</li>
</ul>
<hr>
<ul>
<li><h2 id="os&#x6A21;&#x5757;&#x7684;&#x503C;">os&#x6A21;&#x5757;&#x7684;&#x503C;</h2>
<ul>
<li><h4 id="curdir">curdir</h4>
<pre><code class="lang-py">  os.curdir
  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x8DEF;&#x5F84;   &#x90FD;&#x662F;.
</code></pre>
</li>
<li><h4 id="pardir">pardir</h4>
<pre><code class="lang-py">  os.pardir
  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x4E0A;&#x5C42;&#x76EE;&#x5F55;&#x8DEF;&#x5F84; &#x90FD;&#x662F;..
</code></pre>
</li>
<li><h4 id="path">path</h4>
<pre><code class="lang-py">  os.path
  &#x529F;&#x80FD;&#xFF1A;os&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x6A21;&#x5757;&#xFF0C;&#x64CD;&#x4F5C;&#x975E;&#x5E38;&#x591A;
</code></pre>
</li>
<li><h4 id="sep">sep</h4>
<pre><code class="lang-py">  os.sep
  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;&#x8DEF;&#x5F84;&#x5206;&#x5272;&#x7B26;&#x53F7; window -&gt; \  linux/unix -&gt; /
</code></pre>
</li>
<li><h4 id="extsep">extsep</h4>
<pre><code class="lang-py">  os.sep
  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;&#x8DEF;&#x5F84;&#x5206;&#x5272;&#x7B26;&#x53F7; window -&gt; \  linux/unix -&gt; /
</code></pre>
</li>
<li><h4 id="linesep">linesep</h4>
<pre><code class="lang-py">  os.linesep
  &#x529F;&#x80FD;&#xFF1A;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;&#x6362;&#x884C;&#x7B26;&#x53F7; window -&gt; \r\n  linux/unix -&gt; \n
</code></pre>
</li>
<li><h4 id="environ">environ</h4>
<pre><code class="lang-py">  os.environ
  &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x4FE1;&#x606F;&#x7EC4;&#x6210;&#x7684;&#x5B57;&#x5178;&#xFF0C;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x66F4;&#x6539;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x67E5;&#x8BE2;&#x5F97;&#x5230;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x5BF9;os.environ&#x8FDB;&#x884C;&#x64CD;&#x4F5C;
  &#x8BE5;&#x6A21;&#x5757;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;&#x5747;&#x662F;&#x5B57;&#x5178;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B57;&#x5178;&#x7684;os.environ&#x7684;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;
  &#x6CE8;&#x610F;&#xFF1A;&#x65E0;&#x8BBA;&#x4F7F;&#x7528;os.getenv&#xFF0C;putenv &#x8FD8;&#x662F;&#x4F7F;&#x7528;os.environ&#x8FDB;&#x884C;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x90FD;&#x662F;&#x53EA;&#x5BF9;&#x5F53;&#x524D;&#x811A;&#x672C;&#xFF0C;&#x4E34;&#x65F6;&#x8BBE;&#x7F6E;&#x800C;&#x5DF2;&#xFF0C;&#x65E0;&#x6CD5;&#x76F4;&#x63A5;&#x66F4;&#x65B0;&#x6216;&#x8005;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x8BBE;&#x7F6E;&#x3002;
</code></pre>
</li>
</ul>
</li>
</ul>
<hr>
<ul>
<li><h2 id="os&#x6A21;&#x5757;&#x4E2D;&#x5B50;&#x6A21;&#x5757;path">os&#x6A21;&#x5757;&#x4E2D;&#x5B50;&#x6A21;&#x5757;path</h2>
<ul>
<li><h4 id="ospathabspath">os.path.abspath()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x7684;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;
  &#x683C;&#x5F0F;: os.path.abspath(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span>: &#x662F;&#x7EDD;&#x5BF9;&#x8DEF;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;

  result = os.path.abspath(<span class="hljs-string">&apos;/Users/apple/desktop&apos;</span>)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathdirname">os.path.dirname()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x8DEF;&#x5F84;&#x4E2D;&#x7684;&#x8DEF;&#x5F84;&#x90E8;&#x5206;
  &#x683C;&#x5F0F;: os.path.dirname(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x8FD4;&#x56DE;&#x8DEF;&#x5F84;&#x90E8;&#x5206;

  dname = os.path.dirname(<span class="hljs-string">&apos;/Users/apple/desktop&apos;</span>)
  print(dname)
</code></pre>
</li>
<li><h4 id="ospathbasename">os.path.basename()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x8DEF;&#x5F84;&#x4E2D;&#x6587;&#x4EF6;&#x7684;&#x4E3B;&#x4F53;&#x90E8;&#x5206;(&#x6587;&#x4EF6;&#x540D;.&#x6269;&#x5C55;&#x540D;)
  &#x683C;&#x5F0F;: os.path.basename()
  &#x8FD4;&#x56DE;&#x503C;: &#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x4E3B;&#x4F53;&#x90E8;&#x5206;

  bname = os.path.basename(<span class="hljs-string">&apos;/Users/apple/desktop.ipynb&apos;</span>)
  print(bname)
</code></pre>
</li>
<li><h4 id="ospathjoin">os.path.join()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5C06;&#x4E24;&#x4E2A;&#x8DEF;&#x5F84;&#x8FDE;&#x63A5;&#x8D77;&#x6765;,&#x5408;&#x6210;&#x4E00;&#x4E2A;&#x8DEF;&#x5F84;
  &#x683C;&#x5F0F;: os.path.join(&#x8DEF;&#x5F84;<span class="hljs-number">1</span>,&#x8DEF;&#x5F84;<span class="hljs-number">2</span>)
  &#x8FD4;&#x56DE;&#x503C;: &#x5408;&#x6210;&#x4E4B;&#x540E;&#x7684;&#x8DEF;&#x5F84;

  fullpath = os.path.join(<span class="hljs-string">&apos;/Users/apple&apos;</span>,<span class="hljs-string">&apos;desktop&apos;</span>)
  print(fullpath)
</code></pre>
</li>
<li><h4 id="ospathsplit">os.path.split()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5C06;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x62C6;&#x5206;&#x6210;&#x8DEF;&#x5F84;&#x548C;&#x4E3B;&#x4F53;&#x90E8;&#x5206;,&#x653E;&#x5165;&#x5143;&#x7EC4;&#x4E2D;
  &#x683C;&#x5F0F;: os.path.split(&#x6307;&#x5B9A;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x5143;&#x7EC4; (&#x8DEF;&#x5F84;&#x90E8;&#x5206;,&#x4E3B;&#x4F53;&#x90E8;&#x5206;)

  path1 = <span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>
  tuple1 = os.path.split(path1)
  print(tuple1)
</code></pre>
</li>
<li><h4 id="ospathsplitext">os.path.splitext()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5C06;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x62C6;&#x5C01;&#x6210;&#x8DEF;&#x5F84;&#x4E3B;&#x4F53; &#x548C; &#x6269;&#x5C55;&#x540D;,&#x653E;&#x5165;&#x5143;&#x7EC4;
  &#x683C;&#x5F0F;: os.path.splitext(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x5143;&#x7EC4; (&#x540D;&#x79F0;,&#x6269;&#x5C55;&#x540D;)

  path1 = <span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>
  tuple1 = os.path.splitext(path1)
  print(tuple1)
</code></pre>
</li>
<li><h4 id="ospathgetsize">os.path.getsize()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x5927;&#x5C0F;.&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x5939;&#x5927;&#x5C0F;!
  &#x683C;&#x5F0F;: os.path.getsize(&#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x6587;&#x4EF6;&#x5927;&#x5C0F;

  path1 = <span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>
  size = os.path.getsize(path1)
  print(size)
</code></pre>
</li>
<li><h4 id="ospathisdir">os.path.isdir()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x6587;&#x4EF6;&#x5939;
  &#x683C;&#x5F0F;: os.path.isdir(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span> &#x662F;&#x6587;&#x4EF6;&#x5939;  <span class="hljs-keyword">False</span>  &#x4E0D;&#x662F;&#x6587;&#x4EF6;&#x5939;

  result = os.path.isdir(<span class="hljs-string">&apos;/Users/appple/desktop&apos;</span>)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathisfile">os.path.isfile()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x6587;&#x4EF6;
  &#x683C;&#x5F0F;: os.path.isfile(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span> &#x662F;&#x6587;&#x4EF6;  <span class="hljs-keyword">False</span> &#x4E0D;&#x662F;&#x6587;&#x4EF6;

  result = os.path.isfile(<span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathislink">os.path.islink()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x5FEB;&#x6377;&#x65B9;&#x5F0F;
  &#x683C;&#x5F0F;: os.path.islink(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span> &#x662F;&#x94FE;&#x63A5;  <span class="hljs-keyword">False</span> &#x4E0D;&#x662F;&#x94FE;&#x63A5;

  result = os.path.islink(<span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathgetctime">os.path.getctime()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x521B;&#x5EFA;&#x65F6;&#x95F4;
  &#x683C;&#x5F0F;: os.path.getctime(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x65F6;&#x95F4;&#x6233;

  creattime = os.path.getctime(<span class="hljs-string">&apos;/Users/apple/desktop/test/&apos;</span>)
  print(creattime)
</code></pre>
</li>
<li><h4 id="ospathgetatime">os.path.getatime()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x8BBF;&#x95EE;&#x65F6;&#x95F4;
  &#x683C;&#x5F0F;: os.path.getatime(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x65F6;&#x95F4;&#x6233;

  activetime = os.path.getatime(<span class="hljs-string">&apos;/Users/apple/desktop&apos;</span>)
  print(activetime)
</code></pre>
</li>
<li><h4 id="ospathgetmtime">os.path.getmtime()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x4FEE;&#x6539;&#x65F6;&#x95F4;
  &#x683C;&#x5F0F;: os.path.getmtime(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: &#x65F6;&#x95F4;&#x6233;

  modifytime = os.path.getmtime(&apos;/Users/apple/desktop/01.txt)
  print(modifytime)
</code></pre>
</li>
<li><h4 id="ospathexists">os.path.exists()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x7684;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x6587;&#x4EF6;&#x5939;&#x662F;&#x5426;&#x5B58;&#x5728;
  &#x683C;&#x5F0F;: os.path.exists(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span> &#x5B58;&#x5728;  <span class="hljs-keyword">False</span> &#x4E0D;&#x5B58;&#x5728;

  result = os.path.exists(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathisabs">os.path.isabs()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x6307;&#x5B9A;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;
  &#x683C;&#x5F0F;: os.path.isabs(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;)
  &#x8FD4;&#x56DE;&#x503C;: <span class="hljs-keyword">True</span> &#x662F;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84; <span class="hljs-keyword">False</span> &#x4E0D;&#x662F;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;

  result = os.path.isabs(<span class="hljs-string">&apos;/Users/apple/desktop/01.txt&apos;</span>)
  print(result)
</code></pre>
</li>
<li><h4 id="ospathsamefile">os.path.samefile()</h4>
<pre><code class="lang-py">  &#x529F;&#x80FD;: &#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x8DEF;&#x5F84;&#x6307;&#x5411;&#x7684;&#x662F;&#x4E0D;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;
  &#x683C;&#x5F0F;: os.path.samefile(&#x6307;&#x5B9A;&#x8DEF;&#x5F84;1,&#x6307;&#x5B9A;&#x8DEF;&#x5F84;2)
  &#x8FD4;&#x56DE;&#x503C;: True &#x662F;&#x76F8;&#x540C;&#x6587;&#x4EF6;  False &#x4E0D;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;

  path1 = &apos;../../desktop/01.txt
  path2 = &apos;/Users/apple/desktop/01.txt&apos;
  result = os.path,sanmefile(path1,path2)
  print(result)
</code></pre>
</li>
</ul>
</li>
</ul>
<hr>
<ul>
<li><h2 id="os&#x6A21;&#x5757;&#x9898;&#x76EE;">os&#x6A21;&#x5757;&#x9898;&#x76EE;</h2>
<ul>
<li><h4 id="&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;">&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;</h4>
<pre><code class="lang-py">  <span class="hljs-comment"># -*- coding:utf-8 -*-</span>
  <span class="hljs-keyword">import</span> os

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getAllDirRecursion</span><span class="hljs-params">(path, sp=<span class="hljs-string">&quot;&quot;</span>)</span>:</span>
      filesList = os.listdir(path)
      <span class="hljs-comment"># &#x5904;&#x7406;&#x6BCF;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;</span>
      sp += <span class="hljs-string">&quot;\t&quot;</span>
      <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> filesList:
          <span class="hljs-comment"># &#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x8DEF;&#x5F84;&#xFF08;&#x8981;&#x7528;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#xFF09;</span>
          absPath = os.path.join(path, fileName)
          <span class="hljs-keyword">if</span> os.path.isdir(absPath):
              print(<span class="hljs-string">&quot;&#x76EE;&#x5F55;&#xFF1A;&quot;</span>, fileName)
              getAllDirRecursion(absPath, sp)
          <span class="hljs-keyword">else</span>:
              print(sp+<span class="hljs-string">&quot;&#x666E;&#x901A;&#x6587;&#x4EF6;&#xFF1A;&quot;</span>, fileName)

  <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
      getAllDirRecursion(<span class="hljs-string">r&quot;F:\python\python_learning&quot;</span>)
</code></pre>
</li>
<li><h4 id="&#x6808;&#x6A21;&#x62DF;&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;&#xFF08;&#x5E7F;&#x5EA6;&#x904D;&#x5386;&#xFF09;">&#x6808;&#x6A21;&#x62DF;&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;&#xFF08;&#x5E7F;&#x5EA6;&#x904D;&#x5386;&#xFF09;</h4>
<pre><code class="lang-py">  <span class="hljs-comment"># -*- coding:utf-8 -*-</span>
  <span class="hljs-keyword">import</span> os
  <span class="hljs-keyword">import</span> collections

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getAllDirWide</span><span class="hljs-params">(path)</span>:</span>
      queue = collections.deque()
      <span class="hljs-comment"># &#x8FDB;&#x961F;</span>
      queue.append(path)
      <span class="hljs-keyword">while</span> len(queue) != <span class="hljs-number">0</span>:
          <span class="hljs-comment"># &#x51FA;&#x961F;</span>
          dirPath = queue.popleft()
          <span class="hljs-comment"># &#x627E;&#x51FA;&#x6240;&#x6709;&#x7684;&#x6587;&#x4EF6;</span>
          filesList = os.listdir(dirPath)

          <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> filesList:
              <span class="hljs-comment"># &#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span>
              fileAbsPath = os.path.join(dirPath, fileName)
              <span class="hljs-comment"># &#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x76EE;&#x5F55;&#xFF0C;&#x662F;&#x76EE;&#x5F55;&#x5C31;&#x8FDB;&#x961F;&#xFF0C;&#x4E0D;&#x662F;&#x5C31;&#x6253;&#x5370;</span>
              <span class="hljs-keyword">if</span> os.path.isdir(fileAbsPath):
                  print(<span class="hljs-string">&quot;&#x76EE;&#x5F55;&#xFF1A;&quot;</span> + fileName)
                  queue.append(fileAbsPath)
              <span class="hljs-keyword">else</span>:
                  print(<span class="hljs-string">&quot;&#x666E;&#x901A;&#x6587;&#x4EF6;&#xFF1A;&quot;</span> + fileName)
  <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
      getAllDirWide(<span class="hljs-string">r&quot;F:\python\python_learning&quot;</span>)
</code></pre>
</li>
<li><h4 id="&#x6808;&#x6A21;&#x62DF;&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;&#xFF08;&#x6DF1;&#x5EA6;&#x904D;&#x5386;&#xFF09;">&#x6808;&#x6A21;&#x62DF;&#x9012;&#x5F52;&#x904D;&#x5386;&#x76EE;&#x5F55;&#xFF08;&#x6DF1;&#x5EA6;&#x904D;&#x5386;&#xFF09;</h4>
<pre><code class="lang-py">  <span class="hljs-comment"># -*- coding:utf-8 -*-</span>
  <span class="hljs-keyword">import</span> os

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getAllDirDeep</span><span class="hljs-params">(path)</span>:</span>
      stack = []
      stack.append(path)
      <span class="hljs-comment"># &#x5904;&#x7406;&#x6808;&#xFF0C;&#x5F53;&#x6808;&#x4E3A;&#x7A7A;&#x7684;&#x65F6;&#x5019;&#x7ED3;&#x675F;&#x5FAA;&#x73AF;</span>
      <span class="hljs-keyword">while</span> len(stack) != <span class="hljs-number">0</span>:
          <span class="hljs-comment"># &#x4ECE;&#x6808;&#x91CC;&#x53D6;&#x51FA;&#x6570;&#x636E;</span>
          dirPath = stack.pop()
          <span class="hljs-comment"># print(dirPath)</span>
          <span class="hljs-comment"># &#x76EE;&#x5F55;&#x4E0B;&#x6240;&#x6709;&#x6587;&#x4EF6;</span>
          filesList = os.listdir(dirPath)
          <span class="hljs-comment"># print(filesList)</span>
          <span class="hljs-comment"># &#x5904;&#x7406;&#x6BCF;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x666E;&#x901A;&#x6587;&#x4EF6;&#x5219;&#x6253;&#x5370;&#x51FA;&#x6765;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x76EE;&#x5F55;&#x5219;&#x5C06;&#x8BE5;&#x76EE;&#x5F55;&#x7684;&#x5730;&#x5740;&#x538B;&#x6808;</span>
          <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> filesList:
              fileAbsPath = os.path.join(dirPath, fileName)
              <span class="hljs-keyword">if</span> os.path.isdir(fileAbsPath):
                  <span class="hljs-comment"># &#x662F;&#x76EE;&#x5F55;&#x5C31;&#x538B;&#x6808;</span>
                  print(<span class="hljs-string">&quot;&#x76EE;&#x5F55;&#xFF1A;&quot;</span> + fileName)
                  stack.append(fileAbsPath)
              <span class="hljs-keyword">else</span>:
                  <span class="hljs-comment"># &#x6253;&#x5370;&#x666E;&#x901A;&#x6587;&#x4EF6;</span>
                  print(<span class="hljs-string">&quot;&#x666E;&#x901A;&#x6587;&#x4EF6;&#xFF1A;&quot;</span> + fileName)

  <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
      getAllDirDeep(<span class="hljs-string">r&quot;F:\python\python_learning&quot;</span>)
</code></pre>
</li>
<li><h4 id="&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x5939;&#x5927;&#x5C0F;">&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x5939;&#x5927;&#x5C0F;</h4>
<pre><code class="lang-py">  <span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x83B7;&#x53D6;&#x6587;&#x4EF6;&#x5939;&#x5927;&#x5C0F;</span>
  <span class="hljs-keyword">import</span> os

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_dir_size</span><span class="hljs-params">(path)</span>:</span>
      <span class="hljs-comment"># &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x6587;&#x4EF6;&#x4FE1;&#x606F;</span>
      lists = os.listdir(path)

      <span class="hljs-comment"># &#x521D;&#x59CB;&#x5316;&#x5927;&#x5C0F;&#x8BA1;&#x6570;</span>
      size = <span class="hljs-number">0</span>
      <span class="hljs-comment"># &#x901A;&#x8FC7;&#x62FC;&#x63A5;&#x83B7;&#x53D6;&#x5B8C;&#x6574;&#x8DEF;&#x5F84;</span>
      <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> lists:
          fullpath = os.path.join(path, i)

          <span class="hljs-comment"># &#x5224;&#x65AD;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x6587;&#x4EF6;,&#x662F;&#x6587;&#x4EF6;&#x5219;&#x83B7;&#x53D6;&#x5927;&#x5C0F;&#x5E76;&#x7D2F;&#x52A0;&#x5230;size.</span>
          <span class="hljs-keyword">if</span> os.path.isfile(fullpath) <span class="hljs-keyword">or</span> os.path.islink(fullpath):
              size += os.path.getsize(fullpath)

          <span class="hljs-comment"># &#x5224;&#x65AD;&#x8DEF;&#x5F84;&#x662F;&#x4E0D;&#x662F;&#x6587;&#x4EF6;&#x5939;,&#x5982;&#x679C;&#x662F;&#x6587;&#x4EF6;&#x5939;&#x5219;&#x9012;&#x5F52;&#x8BA1;&#x7B97;&#x5927;&#x5C0F;,&#x7D2F;&#x52A0;&#x5230;size&#x4E2D;</span>
          <span class="hljs-keyword">elif</span> os.path.isdir(fullpath):
              size += get_dir_size(fullpath)
      <span class="hljs-keyword">return</span> size

  <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
      <span class="hljs-comment"># &#x8C03;&#x7528;&#x51FD;&#x6570;</span>
      result = get_dir_size(<span class="hljs-string">r&quot;F:\python\python_learning&quot;</span>)
      print(result/<span class="hljs-number">1024</span>/<span class="hljs-number">1024</span>)
</code></pre>
</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/7.常用模块/2.math模块.html" class="navigation navigation-prev " aria-label="Previous page: math模块"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/7.常用模块/4.zipfile模块.html" class="navigation navigation-next " aria-label="Next page: zipfile模块"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
